package com.wnxy.producer.f_confirm;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    /**
     * 创建队列：queue-confirm
     */
    @Bean("queueConfirm")
    public Queue queueConfirm() {
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange","exchange-dlx");
        map.put("x-dead-letter-routing-key","dlx.ooo");
        map.put("x-max-length",5);
        map.put("x-message-ttl",10000);
        return new Queue("queue-confirm",true,false,false,map);
    }

    /**
     * 创建交换机：exchange-confirm
     */
    @Bean("exchangeConfirm")
    public Exchange exchangeConfirm() {
        return new TopicExchange("exchange-confirm",true,false);
    }

    /**
     * 队列绑定到交换机： confirm.*
     */
    @Bean
    public Binding confirmBinding(
            @Qualifier("queueConfirm") Queue queue,
            @Qualifier("exchangeConfirm") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("confirm.*").noargs();
    }

    /**
     * 死信队列：     queue-dlx
     * 死信交换机：   exchange-dlx
     * 路由key：     dlx.#
     */
    @Bean("queueDlx")
    public Queue queueDlx() {
        return new Queue("queue-dlx",true,false,false);
    }
    @Bean("exchangeDlx")
    public Exchange exchangeDlx() {
        return new TopicExchange("exchange-dlx",true,false);
    }
    @Bean
    public Binding dlxBinding(
            @Qualifier("queueDlx") Queue queue,
            @Qualifier("exchangeDlx") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx.#").noargs();
    }
}
